package com.ellecity06.common.widgets.dialog

import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import com.ellecity06.common.R
import com.ellecity06.common.base.BaseDialog
import com.ellecity06.common.databinding.DialogImageSelectorBinding

/**
 * @author ellecity06
 * @time 2024/10/28 18:12
 * @des
 */

fun Activity.showImageSelector(
    onCamera: (() -> Unit)? = null,
    onGallery: (() -> Unit)? = null
) {
    ImageSelectorDialog(this,  onCamera, onGallery).show()
}

fun Fragment.showImageSelector(
    onCancel: (() -> Unit)? = null,
    onCamera: (() -> Unit)? = null,
    onGallery: (() -> Unit)? = null
) {
    ImageSelectorDialog(requireActivity(),  onCamera, onGallery).show()
}

private class ImageSelectorDialog(
    val activity: Activity,
    private val onCamera: (() -> Unit)? = null,
    private val onGallery: (() -> Unit)? = null
) :
    BaseDialog<DialogImageSelectorBinding>(activity), View.OnClickListener {
    override fun inflaterViewBinding(): DialogImageSelectorBinding {
        return DialogImageSelectorBinding.inflate(LayoutInflater.from(activity))
    }

    override val animation: Int
        get() = R.style.Animation_Design_BottomSheetDialog
    override val gravity: Int
        get() = Gravity.BOTTOM

    override fun initialize() {
        binding.tvCancel.setOnClickListener(this)
        binding.tvFromGallery.setOnClickListener(this)
        binding.tvTakePhoto.setOnClickListener(this)
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            binding.tvCancel.id -> {
                dismiss()

            }
            binding.tvFromGallery.id -> {
                dismiss()
                onGallery?.invoke()
            }
            binding.tvTakePhoto.id -> {
                dismiss()
                onCamera?.invoke()
            }
        }
    }
}